<?php
class InfoApp extends CommonApp {
   
    //个人信息列表
    public function indexAct(){
        $this->checklogin();
		$id = $_SESSION['member']['id'];
        $type = C('type');
        $related = C('related');
        $company_type = C('company_type');
        $result['data'] = DB('info')->where("parent_id={$id}")->select();
        foreach ($result['data'] as &$value) {
            //获取市区
            $value['province'] = M('Address')->getProvince($value['province']);
            //获取城市
            $value['city'] = M('Address')->getCity($value['city']);
            //获取地区
            if(isset($value['area']))
                $value['area'] = M('Address')->getArea($value['area']);
            $value['type'] = $type[$value['type']];
            $value['related'] = $related[$value['related']];
            $value['company_type'] = $company_type[$value['company_type']];
        }
        $this->assign('result',$result);
    	$this->display();
    }

    //添加就业信息页面
    public function modifyAct(){
        //id
        $this->checklogin();
        $id = I('get.id');
        $result = array();
        if(My::is_id($id)){
            //当前id文章数据
            $result = DB('info')->where("id={$id}")->find();
            // $result['params'] = My::decodeJson($result['params']);
            $result['province'] = M('Address')->getProvince($result['province']);
            $result['city'] = M('Address')->getCity($result['city']);
            if(isset($result['area']))
                $result['area'] = M('Address')->getArea($result['area']);
            $this->assign('result',$result);
        }
        //地址二级联动
        $this->assign('province',M('Address')->getProvince());
        $this->assign('city',M('Address')->getCity());
        $this->assign('area',M('Address')->getArea());
        $this->display();
    }

    //添加就业信息
    public function addinfoAct(){
        $this->checklogin();
        if($_POST){
            if(My::arr2var($_POST,'gra_year')=='')
                $this->ajaxReturn(300,"请选择已毕业年份!");
            if(My::arr2var($_POST,'price')=='')
                $this->ajaxReturn(300,"工资填写错误!");
            if(My::arr2var($_POST,'company_name')=='')
                $this->ajaxReturn(300,"公司名称填写错误!");
            if(My::arr2var($_POST,'position')=='')
                $this->ajaxReturn(300,"工作职位填写错误!");
            if(empty($_POST['province']))
                $this->ajaxReturn(300,'请选择省份!');
            if(empty($_POST['city']))
                $this->ajaxReturn(300,'请选择城市!');
            if(My::arr2var($_POST,'type')=='')
                $this->ajaxReturn(300,"工作类型填写错误!");
             if(My::arr2var($_POST,'related')=='')
                $this->ajaxReturn(300,"工作相关度填写错误!");
            $id = $_POST['id'] = My::arr2var($_POST,'id');
            if(My::is_id($id)){//编辑
                /*//白名单
                if($item=='activity')
                    $params = My::keepParams($params,array('table'=>$this->table,'id'=>$id,'verifyId'=>'activity'));*/
                //保存表单模型参数
                //My::setParams($this->table,$params,$id,$item);
                //dump($_POST);exit;
                $_POST['editdate'] = date("Y-m-d H:i:s");
                $ok = DB('info')->clear()->save($_POST);
                if($ok) $this->ajaxReturn(200,"编辑成功");
                else        $this->ajaxReturn(300,"编辑失败");
            }else{//添加
                $_POST['adddate'] = date("Y-m-d H:i:s");
                $_POST['adddate'] = date("Y-m-d H:i:s");
                $_POST['parent_id'] = $_SESSION['member']['id'];
                $_POST['year'] = date('Y');
                $ok = DB('info')->clear()->add($_POST);

                //保存表单模型参数
                //My::setParams($this->table,$params,$ok,$item);
                if($ok>=0)  $this->ajaxReturn(200,"添加成功");
                else        $this->ajaxReturn(300,"添加失败");
            }
        }
    }

    //基本信息填写
    public function modifyinfoAct(){
         //id
        $this->checklogin();
        $id = $_SESSION['member']['id'];
        $result = array();
        if(My::is_id($id)){
            //当前id文章数据
            $result = DB('members')->where("id={$id}")->find();
            $this->assign('result',$result);
        }
       //添加用户界面显示
        $major = M('category')->item2submenu('class',array('level'=>'1'));
        $year = M('category')->item2submenu('year',array('level'=>'1'));
        $this->assign('major',$major);
        $this->assign('year',$year);
        $this->display();
    }

    //基本信息修改
    public function modifyinfo2Act(){
        $this->checklogin();
        if($_POST){
            if(My::arr2var($_POST,'number')=='')
                $this->ajaxReturn(300,"学号填写错误!");
            if(My::arr2var($_POST,'username')=='')
                $this->ajaxReturn(300,"姓名填写错误!");
            if(My::arr2var($_POST,'sex')=='')
                $this->ajaxReturn(300,"性别填写错误!");
            if(!My::is_tel(My::arr2var($_POST,'phone')))
                $this->ajaxReturn(300,"电话填写错误!");
            if(!My::is_email(My::arr2var($_POST,'email')))
                $this->ajaxReturn(300,"邮箱填写错误!");
            if(!My::is_string(My::arr2var($_POST,'political')))
                $this->ajaxReturn(300,"政治面貌填写错误!");
            if(empty($_POST['year']))
                $this->ajaxReturn(300,'请选择毕业年份!');
            if(empty($_POST['major']))
                $this->ajaxReturn(300,'请选择专业!');
            $id = $_SESSION['member']['id'];
            if(My::is_id($id)){//编辑
                $_POST['editdate'] = date("Y-m-d H:i:s");
                $ok = DB('members')->clear()->save($_POST);
                if($ok) $this->ajaxReturn(200,"编辑成功");
                else        $this->ajaxReturn(300,"编辑失败");
            }
        }
    }
    
    //修改密码
    public function modifyipasAct(){
        $this->checklogin();
        if($_POST){
            if(My::arr2var($_POST,'number')=='')
                $this->ajaxReturn(300,"学号填写错误!");
            if(My::arr2var($_POST,'username')=='')
                $this->ajaxReturn(300,"姓名填写错误!");
            if(My::arr2var($_POST,'password')=='')
                $this->ajaxReturn(300,"旧密码不能为空!");
            $password = md5(md5($_POST['password']));
            $number = $_POST['number'];
            $ok = DB('members')->where("number='{$number}' AND password = '{$password}'")->cache()->find();
            if(!$ok)
                $this->ajaxReturn(300,"旧密码填写错误!");
            if(My::arr2var($_POST,'password1')=='')
                $this->ajaxReturn(300,"新密码不能为空!");
            if(My::arr2var($_POST,'password2')=='')
                $this->ajaxReturn(300,"确认新密码不能为空!");
            if(My::arr2var($_POST,'password1') != My::arr2var($_POST,'password2'))
                $this->ajaxReturn(300,"两次密码输入不一致!");
            $data['id'] = $_POST['id'];
            $data['password'] = md5(md5($_POST['password2']));
            $ok = DB('members')->clear()->save($data);
            if($ok){
                unset($_SESSION['member']);
                $this->ajaxReturn(200,"修改成功！");
            }else        $this->ajaxReturn(300,"修改失败！");
        }else $this->display();
    }

    //删除购物车
    public function deleteAct(){
        if($_GET['ids']){
            $ids = explode(',', $_GET['ids']);
            $str = '';
            foreach ($ids as $key => &$value) {
                if(My::is_id($value)){
                    $ok = DB('info')->where("id={$value}")->clear()->delete();
                    if($ok)
                        $str = ($key+1).'条清除成功！';
                    else
                        $str .= '第'.($key+1).'条清除失败！';
                }
            }
            // $member_id = $_SESSION['member']['id'];//用户id'
            // $count = DB('car')->where("member_id = '{$member_id}'")->clear()->count();
            $this->ajaxReturn(200,$str);
        }else $this->ajaxReturn(300,'id参数出错！');
        
    }

    /**
    * 地址联动
    **/
    public function getAddressAct(){
        $area = C('area');
        if($_GET['area_name'] == 'province')
            $result = self::getOption(M('Address')->provinceId2city($_GET['area_id']),'城市','city');
        else if($_GET['area_name'] == 'city')
            $result = self::getOption(M('Address')->cityId2area($_GET['area_id']),'区域','area');
        $this->ajaxReturn(200,$result);
    }

     /**
     * 组装
     */
    private function getOption($result,$address,$city){
        $option = "<option value='' selecct='selected'>请选择".$address."...</option>";
        if($result) {
            foreach ($result as &$value) {
                $option .= "<option  value=".$value[''.$city.'_id'].">".$value[''.$city.'_name']."</option>";
            }
        }
        return $option;
    }
}